package com.ayuku.retail.ui.supplier;

import java.math.BigDecimal;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Shell;

import com.ayuku.commons.FormDialogBasic;
import com.ayuku.commons.Util;
import com.ayuku.commons.UtilPojo;
import com.ayuku.retail.business.Supplier.SupplierImpl;
import com.ayuku.retail.persistence.model.Supplier;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class FormSupplier extends FormDialogBasic {
	private CompSupplier cmp;
	private String custNo;
	private String alamat;
	private String nama;
	private Supplier pjo;

	public FormSupplier(final Shell parent, final int style, final int mode) {
		super(parent, style, mode);
	}

	public void getPanelContent(final Group grpData) {
		cmp = new CompSupplier(grpData, SWT.NONE);
	}

	@Override
	public void additionalProses() {
		getDialogShell().setText("Add/Edit Supplier");
		getDialogShell().setSize(420, 480);
		try {
			// Util.populateCmbGroup(cmpUser.cmbGroup);
		} catch (Exception e) {
			e.printStackTrace();
		}
		Util.setCenter(getDialogShell());
	}

	@Override
	public void clearContols() {
		cmp.txtCode.setText("");
		cmp.txName.setText("");
		cmp.txtAlamat.setText("");
		cmp.txtPhone.setText("");
		cmp.txtFax.setText("");
		cmp.txtWebsite.setText("");
		cmp.txtEmail.setText("");
		cmp.txtSaldoHutang.setValue(BigDecimal.valueOf(0));
		cmp.txtKontakPerson.setText("");
		cmp.txtBankName.setText("");
		cmp.txtAccNo.setText("");
		cmp.txtAccName.setText("");
		cmp.txtCatatan.setText("");
	}

	@Override
	protected void doLoadAdd(String key) {

	}

	@Override
	public void doLoad(String key) throws Exception {
		pjo = SupplierImpl.getById(key);

		cmp.txtCode.setText(pjo.getCode());
		cmp.txName.setText(pjo.getName());
		cmp.txtAlamat.setText(pjo.getAddress());
		cmp.txtPhone.setText(pjo.getPhone());
		cmp.txtFax.setText(pjo.getFax());
		cmp.txtWebsite.setText(pjo.getWebsite());
		cmp.txtEmail.setText(pjo.getEmail());
		cmp.txtSaldoHutang.setValue(pjo.getSaldoHutang());

		cmp.txtKontakPerson.setText(pjo.getContactPerson());
		cmp.txtBankName.setText(pjo.getBankName());
		cmp.txtAccNo.setText(pjo.getBankAccount());
		cmp.txtAccName.setText(pjo.getBankAccName());
		cmp.txtCatatan.setText(pjo.getNotes());

	}

	@Override
	public boolean doValidate() {
		custNo = cmp.txtCode.getText();
		if (Util.isEmpty(custNo)) {
			Util.showMsgBox("Kode supplier harus dilengkapi!", 1,
					getDialogShell());
			cmp.txtCode.setFocus();
			return false;
		}

		nama = cmp.txName.getText();
		if (Util.isEmpty(nama)) {
			Util.showMsgBox("Nama supplier harus dilengkapi!", 1,
					getDialogShell());
			cmp.txName.setFocus();
			return false;
		}

		alamat = cmp.txtAlamat.getText();
		if (Util.isEmpty(alamat)) {
			Util.showMsgBox("Alamat harus dilengkapi!", 1, getDialogShell());
			cmp.txtAlamat.setFocus();
			return false;
		}

		return true;
	}

	@Override
	public void enabledControl(final boolean otherField, final boolean keyField) {
		cmp.txtCode.setEditable(keyField);
		cmp.txName.setEditable(otherField);
		cmp.txtAlamat.setEditable(otherField);
		cmp.txtPhone.setEditable(otherField);
		cmp.txtWebsite.setEditable(otherField);
		cmp.txtEmail.setEditable(otherField);
		cmp.txtFax.setEditable(otherField);
		cmp.txtSaldoHutang.setEnabled(otherField);
		cmp.txtKontakPerson.setEditable(otherField);
		cmp.txtBankName.setEditable(otherField);
		cmp.txtAccNo.setEditable(otherField);
		cmp.txtAccName.setEditable(otherField);
		cmp.txtCatatan.setEditable(otherField);

	}

	@Override
	public int doMainInsert() throws Exception {
		pjo = new Supplier();
		setPojoWithData();
		return SupplierImpl.add(pjo);
	}

	@Override
	public int doMainDelete() throws Exception {
		return SupplierImpl.delete(pjo.getCode());
	}

	@Override
	public int doMainUpdate() throws Exception {
		setPojoWithData();
		return SupplierImpl.update(pjo);

	}

	@Override
	protected void setPojoWithData() throws Exception {
		pjo.setCode(custNo);
		pjo.setName(nama);
		pjo.setAddress(alamat);
		pjo.setPhone(cmp.txtPhone.getText());
		pjo.setFax(cmp.txtFax.getText());
		pjo.setEmail(cmp.txtEmail.getText());
		pjo.setWebsite(cmp.txtWebsite.getText());
		pjo.setContactPerson(cmp.txtKontakPerson.getText());
		pjo.setSaldoHutang(cmp.txtSaldoHutang.getValue());
		pjo.setIsActive("1");
		pjo.setBankName(cmp.txtBankName.getText());
		pjo.setBankAccount(cmp.txtAccNo.getText());
		pjo.setBankAccName(cmp.txtAccName.getText());
		pjo.setNotes(cmp.txtCatatan.getText());

		UtilPojo.setDateAndUser(pjo);
	}

	@Override
	protected void disabledOtherButton(boolean enable) {

	}

}
